﻿using fluentvaly.Exceptions;
using fluentvaly.Results;

namespace fluentvaly
{
    public class Validator : IValidationProvider
    {
        protected ValidatorConfiguration SetUp { get; private set; }

        public Validator(ValidatorConfiguration setUp)
        {
            SetUp = setUp;
        }

        public ValidationResults Validate(object objectToValidate)
        {
            if (!SetUp.IsDefinedFor(objectToValidate.GetType()))
                throw new ValidationNotDefinedException(objectToValidate.GetType());

            var results = new ValidationResults();

            var validationParts = SetUp.GetAllForType(objectToValidate.GetType());

            foreach (var validationPart in validationParts)
            {
                if(!validationPart.CantRun(objectToValidate)) continue;

                results.Add(validationPart.Validate(objectToValidate));
            }

            return results;
        }
    }
}
